<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        v-for="item in propList"
        :key="item.label"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :min-width="item.minWidth"
        show-overflow-tooltip
      />
      <el-table-column
        :label="translate('handle')"
        min-width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('PlatMsgDetailByShop') > -1"
              v-preventReClick
              type="text"
              @click="handleDetail(scope.row)"
            >{{ translate("ckxq") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <el-dialog
      :title="translate('detail')"
      :visible.sync="isShowDetail"
      width="600px"
    >
      <el-form ref="form" label-suffix="：" label-width="auto">
        <el-form-item :label="translate('title')">
          <!-- <el-input v-model="detail.title" disabled /> -->
          <span>{{ detail.title }}</span>
        </el-form-item>
        <el-form-item :label="translate('pubTime')">
          <span>{{ detail.publishTime }}</span>
        </el-form-item>
        <el-form-item :label="translate('content')">
          <!-- <el-input v-model="detail.content" disabled /> -->
          <span>{{ detail.content }}</span>
        </el-form-item>
        <el-form-item v-if="detail.filePath" :label="translate('attachment')">
          <a
            :href="detail.filePath"
          ><el-button type="primary" size="small">{{
            translate("checkAttachment")
          }}</el-button></a>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { terraceMessageList, terraceMessageDetail } from '@/api'
import { propList, queryFormConfig } from './config'
export default {
  data() {
    return {
      propList,
      queryFormConfig,
      listLoading: false,
      list: [],
      meta: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      detail: {},
      isShowDetail: false
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList()
    if (this.$route.query.id) {
      this.handleDetail(this.$route.query)
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
      if (this.$route.query.id) {
        this.handleDetail(this.$route.query)
      }
    })
  },
  methods: {
    handleDetail(row) {
      this.isShowDetail = true
      terraceMessageDetail({ param: { id: row.id }}).then(res => {
        this.detail = { ...res.record }
        this.getNotice()
      })
    },
    getNotice() {
      terraceMessageList({ param: { readFlag: '0' }}).then(res => {
        const list = res.recordList.map(i => {
          if (i.content && i.content.length > 10) {
            i.content = i.content.slice(0, 9) + '...'
          }
          return i
        })
        this.$store.dispatch('app/changeMessageCount', {
          recordList: list,
          totalCount: res.totalCount
        })
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      this.listLoading = true
      terraceMessageList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList.map(i => {
            i.statusName = i.status === '0' ? this.translate('unread') : this.translate('read')
            return i
          })
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style>
</style>
